/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unibo.as.SessionBeans;

import it.unibo.as.HeartsLogic.Player;
import it.unibo.as.client.Table;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;
import it.unibo.as.HeartsLogic.TableGame;
import javax.annotation.PostConstruct;

/**
 *
 * @author Johnny
 */
@Singleton
@LocalBean
public class TablesGameBean {

    private List<TableGame> tableGames;
    private ArrayList<Table> tableList;

    public Boolean createTableGame(String author, String tableID, int AIPlayers, int activePlayers) {
        TableGame newTableGame;

        if(tableGames == null){
            tableGames = new ArrayList<TableGame>();
            tableList = new ArrayList<Table>();
        }

        // check if a table with the same name or author exist
        while(tableGames.iterator().hasNext()){
            if(tableGames.iterator().next().getStrTableID().equals(tableID) || tableGames.iterator().next().getStrOwnerID().equals(author)){
                return false;
            }
        }

       newTableGame = new TableGame(author,
                        tableID,
                        AIPlayers,
                        0
                      );

        tableGames.add(newTableGame);

        tableList.add(newTableGame.getTableInfo());

        return true;
    }

    public TableGame getTableGameFromID(String tableID) {
        TableGame currentTable;

        if(tableGames == null){
            return null;
        }

        while(tableGames.iterator().hasNext()){
            currentTable = tableGames.iterator().next();
            if(currentTable.getStrTableID().equals(tableID)){
                return currentTable;
            }
        }
        return null;
    }
    
    public TableGame getTableGameFromPlayer(String playerID) {
        TableGame currentTable;

        if(tableGames == null){
            return null;
        }

        while(tableGames.iterator().hasNext()){
            currentTable = tableGames.iterator().next();
            for(int c = 0;c < currentTable.getLogic().getPlayerArray().length;c++){
                if(currentTable.getLogic().getPlayerArray()[c].getPlayerID().equals(playerID))
                return currentTable;
            }
        }
        return null;
    }



    public ArrayList<Table> getTableList() {
        if(tableList == null){
            tableList = new ArrayList<Table>();
        }
        return tableList;
    }




    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")


}
